package app.view.component.loading 
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundLoaderContext;
	import flash.net.SharedObject;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import app.view.component.Utils;
	/**
	 * ...
	 * @author HoangBu
	 */
	public class LoaderNonCache extends Loader 
	{
		public static var versionChanged:Boolean = false;
		
		private var _randomToken:String;
		
		private var _forceNonCache:Boolean = false;
		
		public var contextObj:*; ///Đối tượng ngữ cảnh dùng gắn với mỗi đối tượng loader.
		public var contextData:*; ///Dữ liệu thay đổi tùy ngữ cảnh dùng gắn với mỗi đối tượng loader.
		
		public function set Token(value:String):void
		{
			_randomToken = value;
		}
		
		public function get Token():String
		{
			return _randomToken;
		}
		
		public function LoaderNonCache(nonCache:Boolean = false ) 
		{
			_randomToken = Utils.generateRandomString(10);
			_forceNonCache = nonCache;
			contextObj = null;
			contextData = null;
		}
		
		public override function load (request:URLRequest, context:LoaderContext = null) : void
		{
			var requestStr:String;
			var newRequest:URLRequest ;
			var arr:Array = request.url.split('.');
			var txtDuoi:String = arr[arr.length-1];
			if (txtDuoi == "mp3" || txtDuoi == "wma") {
				requestStr = request.url;
				newRequest = new URLRequest(requestStr + "?" + _randomToken);
				
				var context2:SoundLoaderContext = new SoundLoaderContext();
				context2.checkPolicyFile = true;
				var sound:Sound = new Sound();
				sound.load(newRequest, context2);
				sound.addEventListener(Event.COMPLETE, sound_complete);
			}else {
				if (_forceNonCache){
					requestStr = request.url;
					newRequest = new URLRequest(requestStr + "?" + _randomToken);
					super.load(request, context);
				}else{
					requestStr = request.url;
					newRequest = new URLRequest(requestStr + "?" + _randomToken);
					if (versionChanged){
						newRequest = new URLRequest(requestStr + "?" + _randomToken);
					}else{
						newRequest = new URLRequest(requestStr);
					}
					super.load(newRequest, context);
					//super.contentLoaderInfo.addEventListener(Event.COMPLETE, super_complete);
				}
			}	
		}
		
		private function sound_complete(e:Event):void 
		{
			//var sound:Sound = e.currentTarget as Sound;
			//var evt:G2Event = new G2Event(Event.INIT);
			//evt.params = sound;
			//this.dispatchEvent(evt);
		}
		
	}

}